1   /*
2    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3    *
4    * This code is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License version 2 only, as
6    * published by the Free Software Foundation.
7    *
8    * This code is distributed in the hope that it will be useful, but WITHOUT
9    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11   * version 2 for more details (a copy is included in the LICENSE file that
12   * accompanied this code).
13   *
14   * You should have received a copy of the GNU General Public License version
15   * 2 along with this work; if not, write to the Free Software Foundation,
16   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17   *
18   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19   * or visit www.oracle.com if you need additional information or have any
20   * questions.
21   */
22  
23  /*
24   * This file is available under and governed by the GNU General Public
25   * License version 2 only, as published by the Free Software Foundation.
26   * However, the following notice accompanied the original version of this
27   * file:
28   *
29   * Written by Doug Lea with assistance from members of JCP JSR-166
30   * Expert Group and released to the public domain, as explained at
31   * http://creativecommons.org/publicdomain/zero/1.0/
32   */
33  /*
34   * @test
35   * @bug 6785442
36   * @summary Benchmark that tries to GC-tenure head, followed by
37   * many add/remove operations.
38   * @run main GCRetention 12345
39   */
40  
41  import java.util.concurrent.ArrayBlockingQueue;
42  import java.util.concurrent.ConcurrentHashMap;
43  import java.util.concurrent.ConcurrentLinkedDeque;
44  import java.util.concurrent.ConcurrentLinkedQueue;
45  import java.util.concurrent.LinkedBlockingDeque;
46  import java.util.concurrent.LinkedBlockingQueue;
47  import java.util.concurrent.LinkedTransferQueue;
48  import java.util.concurrent.PriorityBlockingQueue;
49  import java.util.LinkedList;
50  import java.util.PriorityQueue;
51  import java.util.ArrayList;
52  import java.util.Collection;
53  import java.util.Collections;
54  import java.util.List;
55  import java.util.Queue;
56  import java.util.Map;
57  
58  public class GCRetention {
59      // Suitable for benchmarking.  Overriden by args[0] for testing.
60      int count = 1024 * 1024;
61  
62      final Map<String,String> results = new ConcurrentHashMap<String,String>();
63  
64      Collection<Queue<Boolean>> queues() {
65          List<Queue<Boolean>> queues = new ArrayList<Queue<Boolean>>();
66          queues.add(new ConcurrentLinkedDeque<Boolean>());
67          queues.add(new ConcurrentLinkedQueue<Boolean>());
68          queues.add(new ArrayBlockingQueue<Boolean>(count, false));
69          queues.add(new ArrayBlockingQueue<Boolean>(count, true));
70          queues.add(new LinkedBlockingQueue<Boolean>());
71          queues.add(new LinkedBlockingDeque<Boolean>());
72          queues.add(new PriorityBlockingQueue<Boolean>());
73          queues.add(new PriorityQueue<Boolean>());
74          queues.add(new LinkedList<Boolean>());
75          queues.add(new LinkedTransferQueue<Boolean>());
76  
77          // Following additional implementations are available from:
78          // http://gee.cs.oswego.edu/dl/concurrency-interest/index.html
79          // queues.add(new SynchronizedLinkedListQueue<Boolean>());
80  
81          // Avoid "first fast, second slow" benchmark effect.
82          Collections.shuffle(queues);
83          return queues;
84      }
85  
86      void prettyPrintResults() {
87          List<String> classNames = new ArrayList<String>(results.keySet());
88          Collections.sort(classNames);
89          int maxClassNameLength = 0;
90          int maxNanosLength = 0;
91          for (String name : classNames) {
92              if (maxClassNameLength < name.length())
93                  maxClassNameLength = name.length();
94              if (maxNanosLength < results.get(name).length())
95                  maxNanosLength = results.get(name).length();
96          }
97          String format = String.format("%%%ds %%%ds nanos/item%%n",
98                                        maxClassNameLength, maxNanosLength);
99          for (String name : classNames)
100             System.out.printf(format, name, results.get(name));
101     }
102 
103     void test(String[] args) {
104         if (args.length > 0)
105             count = Integer.valueOf(args[0]);
106         // Warmup
107         for (Queue<Boolean> queue : queues())
108             test(queue);
109         results.clear();
110         for (Queue<Boolean> queue : queues())
111             test(queue);
112 
113         prettyPrintResults();
114     }
115 
116     void test(Queue<Boolean> q) {
117         long t0 = System.nanoTime();
118         for (int i = 0; i < count; i++)
119             check(q.add(Boolean.TRUE));
120         System.gc();
121         System.gc();
122         Boolean x;
123         while ((x = q.poll()) != null)
124             equal(x, Boolean.TRUE);
125         check(q.isEmpty());
126 
127         for (int i = 0; i < 10 * count; i++) {
128             for (int k = 0; k < 3; k++)
129                 check(q.add(Boolean.TRUE));
130             for (int k = 0; k < 3; k++)
131                 if (q.poll() != Boolean.TRUE)
132                     fail();
133         }
134         check(q.isEmpty());
135 
136         String className = q.getClass().getSimpleName();
137         long elapsed = System.nanoTime() - t0;
138         int nanos = (int) ((double) elapsed / (10 * 3 * count));
139         results.put(className, String.valueOf(nanos));
140     }
141 
142     //--------------------- Infrastructure ---------------------------
143     volatile int passed = 0, failed = 0;
144     void pass() {passed++;}
145     void fail() {failed++; Thread.dumpStack();}
146     void fail(String msg) {System.err.println(msg); fail();}
147     void unexpected(Throwable t) {failed++; t.printStackTrace();}
148     void check(boolean cond) {if (cond) pass(); else fail();}
149     void equal(Object x, Object y) {
150         if (x == null ? y == null : x.equals(y)) pass();
151         else fail(x + " not equal to " + y);}
152     public static void main(String[] args) throws Throwable {
153         new GCRetention().instanceMain(args);}
154     public void instanceMain(String[] args) throws Throwable {
155         try {test(args);} catch (Throwable t) {unexpected(t);}
156         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
157         if (failed > 0) throw new AssertionError("Some tests failed");}
158 }